package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyReferrerReward;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.service.IWdyReferrerRewardService;
import com.ruoyi.system.service.IWdyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 推荐奖励 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-12-26
 */
@Controller
@RequestMapping("/system/wdyReferrerReward")
public class WdyReferrerRewardController extends BaseController
{
    private String prefix = "system/wdyReferrerReward";
	
	@Autowired
	private IWdyReferrerRewardService wdyReferrerRewardService;
	@Autowired
	private IWdyUserService wdyUserService;
	@RequiresPermissions("system:wdyReferrerReward:view")
	@GetMapping()
	public String wdyReferrerReward()
	{
	    return prefix + "/wdyReferrerReward";
	}
	
	/**
	 * 查询推荐奖励列表
	 */
	@RequiresPermissions("system:wdyReferrerReward:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyReferrerReward wdyReferrerReward)
	{
		startPage();
        List<WdyReferrerReward> list = wdyReferrerRewardService.selectWdyReferrerRewardList(wdyReferrerReward);
		for(WdyReferrerReward e : list) {
			if(StringUtils.isNotEmpty(e.getCreateBy())) {
				QueryWrapper<WdyUser> wdyUserQueryWrapper = new QueryWrapper<>();
				wdyUserQueryWrapper.eq("user_id", e.getCreateBy());
				WdyUser user = wdyUserService.getOne(wdyUserQueryWrapper);
				e.addExtraData("userName", user.getUserName());
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出推荐奖励列表
	 */
	@RequiresPermissions("system:wdyReferrerReward:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyReferrerReward wdyReferrerReward)
    {
    	List<WdyReferrerReward> list = wdyReferrerRewardService.selectWdyReferrerRewardList(wdyReferrerReward);
        ExcelUtil<WdyReferrerReward> util = new ExcelUtil<WdyReferrerReward>(WdyReferrerReward.class);
        return util.exportExcel(list, "wdyReferrerReward");
    }
	
	/**
	 * 新增推荐奖励
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存推荐奖励
	 */
	@RequiresPermissions("system:wdyReferrerReward:add")
	@Log(title = "推荐奖励", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyReferrerReward wdyReferrerReward)
	{		
		return toAjax(wdyReferrerRewardService.insertWdyReferrerReward(wdyReferrerReward));
	}

	/**
	 * 修改推荐奖励
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyReferrerReward wdyReferrerReward = wdyReferrerRewardService.selectWdyReferrerRewardById(id);
		mmap.put("wdyReferrerReward", wdyReferrerReward);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存推荐奖励
	 */
	@RequiresPermissions("system:wdyReferrerReward:edit")
	@Log(title = "推荐奖励", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyReferrerReward wdyReferrerReward)
	{		
		return toAjax(wdyReferrerRewardService.updateWdyReferrerReward(wdyReferrerReward));
	}
	
	/**
	 * 删除推荐奖励
	 */
	@RequiresPermissions("system:wdyReferrerReward:remove")
	@Log(title = "推荐奖励", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyReferrerRewardService.deleteWdyReferrerRewardByIds(ids));
	}
	
}
